/*
* @Author: steven.wang
* @Date:   2018-12-26 16:35:53
* @Last Modified by:   steven.wang
* @Last Modified time: 2019-03-23 04:21:10
*/
const fs = require("fs");
const superagent = require('superagent');
const cheerio = require('cheerio');
const asyncQuene = require("async").queue;
require('superagent-charset')(superagent);
const query = require('./db');

/**
 * 异步获取页面属性
 * @author   steven.wang
 * @DateTime 2018-12-26T16:05:57+0800
 * @param    {string}                 url 链接
 * @return   {Promise}
 */
const getHtmlAsync = (url, charset = 'utf-8', cookie = '') => {
    return new Promise((resolve, reject) => {
        superagent.get(url)
            .buffer()
            .charset(charset)
            .set('Cookie', cookie)
            .then(res => {
            if (res.ok) {
                resolve(cheerio.load(res.text, {decodeEntities: false}))
            } else {
                reject(res.err);
            }
        }, err => {
            reject(err);
        });
    });
}
/**
 * 获取拉勾网的cookie
 * @author steven.wang
 * @DateTime 2019-03-22T23:26:31+0800
 */
const getLagouCookie = (url, cookie = '') => {
    return new Promise((resolve, reject) => {
        superagent.get(url)
            .set('Cookie', cookie)
            .then(res => {
            if (res.ok) {
                resolve(res.headers['set-cookie'].join(';'))
            } else {
                reject(res.err);
            }
        }, err => {
            reject(err);
        });
    });
}
/**
 * 异步POST获取接口数据
 * @author steven.wang
 * @DateTime 2019-03-22T23:26:31+0800
 */
const postAjaxAsync = (url, data = '', cookie = '') => {
	return new Promise((resolve, reject) => {
		superagent.post(url)
            .set('cookie', cookie)
            .set('content-type', 'application/x-www-form-urlencoded')
            .set('host', 'www.lagou.com')
            .set('referer', 'https://www.lagou.com/jobs/list_HTML5?px=default&city=%E4%B8%8A%E6%B5%B7&pn=4')
            .set('accept', 'application/json, text/javascript, */*; q=0.01')
            .set('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36')
            .send(JSON.stringify(data))
            .then(res => {
			if (res.ok) {
				resolve(res.text)
			} else {
				reject(res.err);
			}
		}, err => {
			reject(err);
		});
	});
}
/**
 * 将画册信息保存为JSON
 * @author steven.wang
 * @DateTime 2019-02-27T11:04:44+0800
 */
const writeJsonToFile = (path, data) => {
    try {
        fs.writeFileSync(path, JSON.stringify(data));
    } catch(e) {
        // statements
        throw new Error('写入' + path + '失败');
    }
}
/**
 * 职位信息存入数据库
 * @author steven.wang
 * @DateTime 2019-03-23T03:15:03+0800
 */
const insertJob = values => {
    return query(`insert into job (source, name, state, type, label, work_time, publish_time, salary, city, experience, education, advantage, description, address, hr_name, hr_position, company_name, company_id, company_detail_url) values ?`, [values]);
}


module.exports = {
	getHtmlAsync,
    getLagouCookie,
    postAjaxAsync,
	writeJsonToFile,
	insertJob
}